import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'


/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard',title:'首页' }
    }]
  },


  {
    path: '/hot',
    component: Layout,
    redirect: '/hot/travelhot',
    children: [{
      path: 'travelhot',
      name: 'travelhot',
      component: () => import('@/views/hot/index'),
      meta: { title: '热门景区', icon: 'el-icon-star-off'}
    }]
  },
   {
    path: '/new',
    component: Layout,
    redirect: '/new/travelnew',
    children: [{
      path: 'travelnew',
      name: 'travelnew',
      component: () => import('@/views/new/index'),
      meta: { title: '旅游快讯', icon: 'el-icon-picture-outline'}
    }]
  },
  {
    path: '/strategy',
    component: Layout,
    redirect: '/strategy/travelstrategy',
    children: [{
      path: 'travelstrategy',
      name: 'travelstrategy',
      component: () => import('@/views/strategy/index'),
      meta: { title: '旅游攻略', icon: 'el-icon-camera'}
    }]
  },
  

  

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const $router = new Router({
  mode: 'history', // 去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})
 
$router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g;
  const isChunkLoadFailed = error.message.match(pattern);
  if(isChunkLoadFailed){
    // 用路由的replace方法，并没有相当于F5刷新页面，失败的js文件并没有从新请求，会导致一直尝试replace页面导致死循环，而用 location.reload 方法，相当于触发F5刷新页面，虽然用户体验上来说会有刷新加载察觉，但不会导致页面卡死及死循环，从而曲线救国解决该问题
      location.reload();
      // const targetPath = $router.history.pending.fullPath;
      // $router.replace(targetPath);
  }
  



const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
